package com.example.android.camera2video;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.content.Intent;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.view.MenuItem;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity {
	public final static int MPEG4=0;
    public final static int MPEG2_TS=1;
    public final static int OGG=2;
    public final static int WEBM=3;
    public final static int ThreeGPP=4;

    public final static int H264=0;
    public final static int H263=1;
    public final static int MPEG4_SP=2;
    public final static int VP8=3;
    public final static int HEVC=4;

    public final static int ACC=0;
    public final static int AMR_NB=1;
    public final static int AMR_WB=2;

    public static final int PICKFILE_RESULT_CODE = 1;    
	public static final String LOG_TAG = "Setting";
    private RadioGroup radioGroup_format;
    private  int format;
    private RadioGroup radioGroup_video_encoder;
    private RadioGroup radioGroup_audio_encoder;
    private int video_encoder;
    private int audio_encoder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        format= (int) extras.getLong("format",0);
        video_encoder= (int) extras.getLong("venc",  0);
        audio_encoder= (int) extras.getLong("aenc",  0);

        radioGroup_format = (RadioGroup) findViewById(R.id.radioGroup_format);
        radioGroup_format.clearCheck();
        radioGroup_format.check(
                format==ThreeGPP?R.id.three_gpp:
                format==MPEG4?R.id.mpeg4:
                format==MPEG2_TS?R.id.mpeg2:
                format==OGG?R.id.ogg:
                R.id.webm );

        radioGroup_format.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.mpeg4) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    format = MPEG4;
                } else if (checkedId == R.id.mpeg2) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    format = MPEG2_TS;
                } else if (checkedId == R.id.ogg) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    format = OGG;
                } else if (checkedId == R.id.webm) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    format = WEBM;
                } else if (checkedId == R.id.three_gpp) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    format = ThreeGPP;
                }
            }
        });

        radioGroup_video_encoder = (RadioGroup) findViewById(R.id.radioGroup_video_encoder);
        radioGroup_video_encoder.clearCheck();
        radioGroup_video_encoder.check(
                video_encoder==HEVC?R.id.hevc:
                 video_encoder==VP8?R.id.vp8:
                video_encoder==MPEG4_SP?R.id.mpeg4_sp:
                video_encoder==H264?R.id.h264:
                R.id.h263);

        radioGroup_video_encoder.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.h264) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    video_encoder = H264;
                } else if (checkedId == R.id.h263) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    video_encoder = H263;
                } else if (checkedId == R.id.mpeg4_sp) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    video_encoder = MPEG4_SP;
                } else if (checkedId == R.id.vp8) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    video_encoder = VP8;
                } else if (checkedId == R.id.hevc) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    video_encoder = HEVC;
                }
            }
        });

        radioGroup_audio_encoder = (RadioGroup) findViewById(R.id.radioGroup_audio_encoder);
        radioGroup_audio_encoder.clearCheck();
        radioGroup_audio_encoder.check(
                audio_encoder==ACC?R.id.acc:
                audio_encoder==AMR_NB?R.id.amr_n:
                R.id.amr_w);

        radioGroup_audio_encoder.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.acc) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    audio_encoder = ACC;
                } else if (checkedId == R.id.amr_n) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    audio_encoder = AMR_NB;
                }else if (checkedId == R.id.amr_w) {
                    //RadioButton rb = (RadioButton) group.findViewById(checkedId);
                    //Toast.makeText(SettingActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
                    audio_encoder = AMR_WB;
                }
            }
        });
    }

    public void onOk(View view) {
        Intent intent=new Intent();
        Bundle extras =    new Bundle();
        extras.putLong("format",format);
        extras.putLong("venc",video_encoder);
        extras.putLong("aenc",audio_encoder);
        intent.putExtras(extras);
        setResult(2,intent);
        finish();//finishing activity
    }
}